/*
@title: Formula 1
@description: "Formula 1" is a multiplayer racing game where two players control race cars, Ferrari and Red Bull, competing in a championship across multiple tracks. Players navigate the cars to reach the finish line while avoiding obstacles like pits, birds, and cones. The car with the most wins at the end of the races is crowned the champion.
@author: Mary
@tags: ['multiplayer']
@addedOn: 2024-04-12
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const ferrari = "f"
const redbull = "r"
const road = "d"
const grass = "g"
const mushroom = "t"
const pit = "p"
const blueFlower = "b"
const finishLine = "l"
const con = "c"
const water = "w"
const bridge = "a"
const finishLine1 = "e"
const monaco = "h"
const netherlands = "n"
const austria = "q"
const japan = "i"
const monza = "j"
const belgium = "k"
const abuDhabi = "m"
const bird = "o"
const spiderman = "s"
const prize = "u"
const black = "v"
const pinkFlower = "z"

const pit1 = tune `
500: C4^500,
15500`
const bird1 = tune`
500: B5/500,
15500`
const finish = tune `
89.02077151335311: G4^89.02077151335311,
89.02077151335311: B4^89.02077151335311,
89.02077151335311: C5^89.02077151335311,
89.02077151335311: D5^89.02077151335311,
2492.581602373887`
const finish1 = tune`
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093 + B4~340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093,
340.90909090909093: E4^340.90909090909093 + G4^340.90909090909093 + C5-340.90909090909093,
340.90909090909093: A4-340.90909090909093 + F4^340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4~340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093,
340.90909090909093: G4^340.90909090909093 + E4^340.90909090909093 + C5-340.90909090909093,
340.90909090909093: G4-340.90909090909093 + C4^340.90909090909093 + E4^340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093 + B4~340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093,
340.90909090909093: E4^340.90909090909093 + G4^340.90909090909093 + C5-340.90909090909093,
340.90909090909093: F4-340.90909090909093 + C4^340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093 + A4~340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093,
340.90909090909093: E4^340.90909090909093 + G4^340.90909090909093 + C5-340.90909090909093,
340.90909090909093: F4^340.90909090909093 + A4-340.90909090909093 + D4^340.90909090909093,
340.90909090909093: G4^340.90909090909093 + C5^340.90909090909093 + E5-340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093 + A4~340.90909090909093,
340.90909090909093: C4-340.90909090909093 + E4^340.90909090909093 + G4~340.90909090909093,
340.90909090909093: E4^340.90909090909093 + G4^340.90909090909093 + C5-340.90909090909093,
340.90909090909093: E4^340.90909090909093 + G4-340.90909090909093,
340.90909090909093: F4-340.90909090909093 + C4^340.90909090909093,
340.90909090909093: C4^340.90909090909093 + F4~340.90909090909093,
340.90909090909093: D4^340.90909090909093 + B4~340.90909090909093,
340.90909090909093: D4^340.90909090909093 + F4~340.90909090909093,
340.90909090909093: D4^340.90909090909093,
340.90909090909093: D4^340.90909090909093 + F4~340.90909090909093,
340.90909090909093: D4^340.90909090909093,
340.90909090909093: D4^340.90909090909093 + G4^340.90909090909093 + C5-340.90909090909093,
340.90909090909093: F4^340.90909090909093,
340.90909090909093: F4^340.90909090909093 + D4-340.90909090909093,
340.90909090909093: G4^340.90909090909093 + C5-340.90909090909093`
const background = tune `
245.9016393442623: E4~245.9016393442623,
245.9016393442623: F4~245.9016393442623,
245.9016393442623: G4~245.9016393442623 + E4~245.9016393442623,
245.9016393442623: A4~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + B4~245.9016393442623,
245.9016393442623: C5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + D5~245.9016393442623,
245.9016393442623: E5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + F5~245.9016393442623,
245.9016393442623: G5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + A5~245.9016393442623,
245.9016393442623: B5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + A5~245.9016393442623,
245.9016393442623: G5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + F5~245.9016393442623,
245.9016393442623: E5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + D5~245.9016393442623,
245.9016393442623: C5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + B4~245.9016393442623,
245.9016393442623: A4~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + G4~245.9016393442623,
245.9016393442623: F4~245.9016393442623,
245.9016393442623: E4~245.9016393442623,
245.9016393442623: F4~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + G4~245.9016393442623,
245.9016393442623: A4~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + B4~245.9016393442623,
245.9016393442623: C5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + B4~245.9016393442623,
245.9016393442623: A4~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + G4~245.9016393442623,
245.9016393442623: F4~245.9016393442623`
const playback = playTune(background, Infinity)

  setLegend(
  [ferrari, bitmap`
................
................
................
................
................
.....337777.....
.0.333777777....
0336333777777...
336333666333333.
3330036363300330
.301106666011030
..0110....0110..
...00......00...
................
................
................` ],
  [ redbull, bitmap `
................
................
................
................
................
.....557777.....
...5H5777777....
.555H55777777...
5555H5H5HH55555.
555005H5HH500555
.501105H5H011055
..0110....0110..
...00......00...
................
................
................`],
  [ road, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
  [ grass, bitmap`
4444444444444444
444D444444444444
4444D44444444444
4444D44444444444
44444444444D4444
44444444444D4444
4444D44444444444
444D44444444444D
444D4444444444D4
44444444444444D4
444444444D444444
4444444444D44444
4444444D44444444
444444D444444444
444444D444444444
4444444444444444`],
  [ mushroom,  bitmap`
4444444444444444
444D444444444444
44D444444444D444
44D444444444D444
4444423332444444
4444333333344444
4442333233324444
4443333333334444
444CC23332CC4D44
44444CCCCC444444
4D44442224444444
44444422244444D4
44D44422244D4444
44D4441214D44444
4444444444D44444
4444444444444444`],
  [ pit, bitmap`
CCCCCCCCCCCCCCCC
FCCC00000000CCCC
CCC0000000000CCC
CC000000000000CC
C00000000000000C
C00000000000000C
C00000000000000C
C00000000000000C
C00000000000000C
C00000000000000C
C00000000000000C
C00000000000000C
CC000000000000CC
CCC0000000000CFC
CFCC00000000CCCC
CCCCCFCCCCCCCCCC`],
  [ blueFlower, bitmap`
4444444444444444
44444754444444D4
44444774D4444D44
444444D4DD477D44
44477D444D477774
4447777444556774
44556774445555DD
4455554D4D445544
44445544D4774444
4DDD44D47777DD44
44DDD4D4776554D4
4444DD44D5555444
44444D4D45544444
444444D444444444
444444D444444444
4444444444444444`],
  [ finishLine, bitmap`
0000222200002222
0000222200002222
0000222200002222
0000222200002222
2222000022220000
2222000022220000
2222000022220000
2222000022220000
0000222200002222
0000222200002222
0000222200002222
0000222200002222
2222000022220000
2222000022220000
2222000022220000
2222000022220000`],
  [ con,  bitmap`
.......99.......
......9999......
.....999999.....
.....999999.....
....99999999....
....66666666....
....66666666....
....99999999....
...9999999999...
...9999999999...
..666666666666..
..666666666666..
.99999999999999.
9999999999999999
9999999999999999
9999999999999999`],
  [ water, bitmap`
7777777777777777
7777777775555777
7777777777777777
5555577777777777
7777777777777777
7777777777777777
7777777777777777
7777555555577557
7777777777777777
7777777777777777
7777777777777777
7777777777755777
7755577777777777
7777777777777777
7777777777777777
7777777777777777`],
  [ bridge, bitmap`
7777777775577777
7777777777777777
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
7775555577777777
7777777777775577`],
  [ finishLine1, bitmap`
2222000022220000
2222000022220000
2222000022220000
2222000022220000
0000222200002222
0000222200002222
0000222200002222
0000222200002222
2222000022220000
2222000022220000
2222000022220000
2222000022220000
0000222200002222
0000222200002222
0000222200002222
0000222200002222`],
  [ monaco, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
  [ japan, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222223333222222
2222233333322222
2222233333322222
2222233333322222
2222233333322222
2222223333222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
  [ monza, bitmap`
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333
DDDDD22222233333`],
  [ belgium, bitmap`
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333
0000066666633333`],
  [ abuDhabi, bitmap`
33333DDDDDDDDDDD
33333DDDDDDDDDDD
33333DDDDDDDDDDD
33333DDDDDDDDDDD
33333DDDDDDDDDDD
3333322222222222
3333322222222222
3333322222222222
3333322222222222
3333322222222222
3333322222222222
3333300000000000
3333300000000000
3333300000000000
3333300000000000
3333300000000000`],
  [ bird, bitmap`
................
................
.....00000......
....0222220.....
..00L0000L00....
..0L0010L000....
..0000100020....
..0999922220....
...0222222120...
..012222212220..
..012222212220..
..0112222112200.
..0111111111110.
...00111111000..
.....0900090....
.....090.090....`],
  [ spiderman, bitmap`
0000000000000000
000LLLLL00000000
00L33333L0000000
0L3333333L000000
0LL33333LL000000
0L2L333L2L000000
0L22L3L22L000000
0L22L3L22L000000
00LL333LL0000000
0L3LLLLL3L000000
L3733L3373L00000
L3L3L3L3L3L00000
0LL73337LL000000
00L77777L0000000
00L33L33L0000000
000LL0LL00000000`],
  [ prize, bitmap`
0000000000000000
0000000000000000
0000000000000000
00000666666FF000
00666622666FF66F
00600626666FF00F
00600626666FF00F
00066666666FFFF0
00000666666FF000
0000006666FF0000
000000066FF00000
000000006F000000
000000006F000000
000000006F000000
000000066FF00000
00000066FFFF0000`],
  [ black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [ pinkFlower, bitmap`
444884D488444444
4444D4D488844444
4D444DD44D444444
4444884DD4444444
44444D4D4444D444
44D444DD48444D44
4D44884D88844444
44448D4D4D8444D4
444444DDD4444D44
4448844D44444D44
4448D4D4D4444444
44488DDD44444444
4D4444D448844D44
44D4444D4D844D44
44D4444DD4444444
4444444D44444444`],
  [ netherlands, bitmap `
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
5555555555555555
5555555555555555
5555555555555555
5555555555555555
5555555555555555`],
  [ austria, bitmap `
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`]
)


setSolids([ferrari, redbull, grass, mushroom, blueFlower, con, pinkFlower, water, monaco, japan,
           monza,netherlands, austria, belgium, abuDhabi, black])
setBackground(road)

let level = 0
let fwins = 0
let rbwins = 0

const levels = [
  map`
iiiiiiiiiiiiiiiiiiii
ddpddddddpdddpddddpl
fddpddpdddddpdddpddl
dddddpddppddddpddddl
gbgtgzgtgbggtggtggtz
tgzgbgggzgtgggbgzbgg
ddddpdpddddpddpddpdl
rdpdddpddpddpdddpddl
ddddpddddpddddpdddpl
iiiiiiiiiiiiiiiiiiii`, map`
hhhhhhhhhhhhhhhhhhhh
ggodddpgzbggpdddddzb
fpddpdddggzgdddpddgt
dddwwwpdddcdddgtdptg
gbwwwwwdpddddptbdddl
tzwwwwwddddpddzgdddl
ddpwwwddcbggddbtpdtz
rddpdddogtgbdpddddzt
zbddddpgbgzgdddddpgb
hhhhhhhhhhhhhhhhhhhh`,map `
qqqqqqqqqqqqqqqqqqqq
cdddodgzbwtodpddggbg
fdpddctgwwwdddddgtgz
cdddddwwwwwpdwpdwggg
bggdpdaaaaaddwddwdpl
gtzoddaaaaadpwdpwddl
cdddddwwwwwwwwddwpdg
rdpddogbgzgtwwpddcdt
cdddddggtggbgwdpdddg
qqqqqqqqqqqqqqqqqqqq`, map`
nnnnnnnnnnnnnnnnnnnn
ddddoddzgtggtgzbggtg
fdpdddogbggzgbgtgzbz
ddcddddddcddodpddodl
ggbtzdoddddddddodddl
zgzggddpddpdoddddpdl
dddodddoddddddpdddol
rdpddodtggtgzbgggtgz
ddddcddgzgbbggzgtggb
nnnnnnnnnnnnnnnnnnnn`, map`
jjjjjjjjjjjjjjjjjjjj
zgtgggbggzzgggtggzgg
fddodcdddddddpdddddl
dddddddddcpdddddpddl
ddcddpdtgddddodcddpl
cpdddcdbtdoddddddpdl
dddpddddddpdcpdpdddl
rdodddpddddpddddddpl
bgzgtgggzgggbzggbgzg
jjjjjjjjjjjjjjjjjjjj`,
  map`
kkkkkkkkkkkkkkkkkkkk
ddpdddcwwwwcdpddcddl
fdddpdddaaaadddddpdl
cdddddpcwwwwcddpdddl
ddgbddggwwwwwzgtgzbg
ddtgdczggwwwwgbgzgtg
cdddddddpcwwwwddddcl
rpddpddddddaaaadpdpl
dddpddcddddcwwwwdddl
kkkkkkkkkkkkkkkkkkkk`, map`
mmmmmmmmmmmmmmmmmmmm
gtgbgzgggtggzbgzgtgg
fpdpdddpdddpdddpddde
dddddpdodpdddpdddpde
cpdpdddpdddpdddpddde
cpdddpdddpdddpdddpde
dddpdddpdddpdodpddde
rpdddpdddpdddpdddpde
gzggtgzgbggzggtgzbgz
mmmmmmmmmmmmmmmmmmmm`]

setMap(levels[level])

addText("Hello!", { x:7, y:7, color: color `0`})
addText("Use:", { x:8, y:8, color: color`0`})
addText("w,a,s,d", { x:12, y:4, color: color`2`})
addText("for", {x:14, y:5, color: color`2`})
addText("Ferrari", {x:12, y:6, color: color`3`})
addText("i,j,k,l", { x:12, y:9, color: color`2`})
addText("for", {x:14, y:10, color: color`2`})
addText("RedBull", {x:12, y:11, color: color`7`})


onInput("w", () => {
  getFirst(ferrari).y -= 1
})

onInput("s", () => {
  getFirst(ferrari).y += 1
})

onInput("d", () => {
  getFirst(ferrari).x += 1
})

onInput("a", () => {
  getFirst(ferrari).x -= 1
})

onInput("i", () => {
  getFirst(redbull).y -= 1
})

onInput("k", () => {
  getFirst(redbull).y += 1
})

onInput("l", () => {
  getFirst(redbull).x += 1
})

onInput("j", () => {
  getFirst(redbull).x -= 1
})

afterInput(() => {
  try{let ferrariSprite = getFirst(ferrari)
if(getTile(ferrariSprite.x, ferrariSprite.y)[1].type == pit){
  playTune(pit1)
  clearTile(ferrariSprite.x, ferrariSprite.y)
  addSprite(ferrariSprite.x, ferrariSprite.y, pit)
  addSprite(0, 2, ferrari)}

if(getTile(ferrariSprite.x, ferrariSprite.y)[1].type == bird){
  playTune(bird1)
  clearTile(ferrariSprite.x, ferrariSprite.y)
  addSprite(ferrariSprite.x, ferrariSprite.y, con)
  addSprite(0,2, ferrari)
}
      
if (tilesWith(ferrari, finishLine).length==1) {
playTune(finish)
level++;
fwins++;
setMap(levels[level]); 
}

  if(fwins >= 3){
  if(getTile(ferrariSprite.x, ferrariSprite.y)[1].type == finishLine1){
    playback.end()
    playTune(finish1)
    addText("FERRARI", {x:6, y:7, color: color `2`})
    addText("WON", {x:7, y:8, color: color `2`})
    clearTile(6, 4);
    addSprite(6, 4, black)
    clearTile(7, 4); 
    addSprite(7, 4, black)
    clearTile(8, 4); 
    addSprite(8, 4, black)
    clearTile(9, 4); 
    addSprite(9, 4, black)
    clearTile(10, 4); 
    addSprite(10, 4, black)
    clearTile(11, 4); 
    addSprite(11, 4, black)
    clearTile(12, 4);
    addSprite(12, 4, black)
    clearTile(7, 5); 
    addSprite(7, 5, black)
    clearTile(8, 5); 
    addSprite(8, 5, black)
    clearTile(9, 5); 
    addSprite(9, 5, black)
    clearTile(10,5); 
    addSprite(10, 5, prize)
    clearTile(11,5); 
    addSprite(11, 5, spiderman)
    
  }}
      
if(getTile(ferrariSprite.x, ferrariSprite.y)[1].type == road){
  clearText()
}
}catch(e){}
  
try{
  let redbullSprite = getFirst(redbull)
if(getTile(redbullSprite.x, redbullSprite.y)[1].type == pit){
  playTune(pit1)
  clearTile(redbullSprite.x, redbullSprite.y)
  addSprite(redbullSprite.x, redbullSprite.y, pit)
  addSprite(0, 7, redbull)
}

if(getTile(redbullSprite.x, redbullSprite.y)[1].type == bird){
  playTune(bird1)
  clearTile(redbullSprite.x, redbullSprite.y)
  addSprite(redbullSprite.x, redbullSprite.y, con)
  addSprite(0, 7, redbull)
}
  
if (tilesWith(redbull, finishLine).length==1) {
level++;
rbwins++;
setMap(levels[level]) 
}
 if(rbwins >= 3){
   if(getTile(redbullSprite.x, redbullSprite.y)[1].type == finishLine1){
     playback.end()
     playTune(finish1)
     addText("REDBULL", {x:6, y:7, color: color `2`})
    addText("WON", {x:7, y:8, color: color `2`})
    clearTile(6, 4);
    addSprite(6, 4, black)
    clearTile(7, 4); 
    addSprite(7, 4, black)
    clearTile(8, 4); 
    addSprite(8, 4, black)
    clearTile(9, 4); 
    addSprite(9, 4, black)
    clearTile(10, 4); 
    addSprite(10, 4, black)
    clearTile(11, 4); 
    addSprite(11, 4, black)
    clearTile(12, 4);
    addSprite(12, 4, black)
    clearTile(7, 5); 
    addSprite(7, 5, black)
    clearTile(8, 5); 
    addSprite(8, 5, black)
    clearTile(9, 5); 
    addSprite(9, 5, black)
    clearTile(10,5); 
    addSprite(10, 5, prize)
    clearTile(11,5); 
    addSprite(11, 5, spiderman)
   }}
  if(getTile(redbullSprite.x, redbullSprite.y)[1].type == road){
    clearText()
  }
}catch(e){}

});
